<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="icon" href="/ui/favicon.ico" />
    <link rel="manifest" href="/ui/CPack.webmanifest" />
    <script src="/ui/libs/vue.min.js"></script>
    <script src="/ui/libs/vuex.min.js"></script>
    <script src="/ui/libs/vue-router.min.js"></script>
    <script src="/ui/libs/jsencrypt.min.js"></script>
    <script src="/ui/watermark.js"></script>
    <link rel='preload' as='script' href="/ui/libs/bk-magic-vue/bk-magic-vue.min.js" />
    <link href="/ui/libs/bk-magic-vue/bk-magic-vue.min.css" rel="stylesheet">
    <link href="/ui/iconfont.css" rel="stylesheet">
    <title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
    <script>
        // 请求svg图标
        const ajax = new XMLHttpRequest()
        ajax.open('GET', '/ui/sprite.svg', true)
        ajax.onload = function (e) {
            const div = document.createElement('div')
            div.setAttribute('style', 'position: absolute; width: 0; height: 0; overflow: hidden;')
            div.innerHTML = e.target.responseText
            document.body.prepend(div)
        }
        ajax.send()
    </script>
    <script>
        if ('serviceWorker' in navigator) {
            navigator.serviceWorker.register('/ui/sw.js');
        }
        function isPWA() {
            return ["fullscreen", "standalone", "minimal-ui"].some(
                (displayMode) => window.matchMedia('(display-mode: ' + displayMode + ')').matches
            )
        }
        function loadLibScript (src) {
            return new Promise((resolve, reject) => {
                const script = document.createElement('script')
                script.setAttribute('type', 'text/javascript')
                script.setAttribute('src', src)
                document.head.append(script)
                script.onload = () => resolve()
                script.onerror = () => reject()
            })
        }
    </script>
    <script>
        var PAAS_SERVICE_URL = '__BK_HTTP_SCHEMA__://__BK_REPO_PAAS_FQDN__'
        var DEVOPS_SITE_URL = '__BK_HTTP_SCHEMA__://__BK_CI_FQDN__'
        var LOGIN_SERVICE_URL = /^https?/.test(PAAS_SERVICE_URL)
            ? PAAS_SERVICE_URL + '/login/'
            : '__BK_REPO_PAAS_LOGIN_URL__'
        // standalone: 独立部署模式
        // ci: ci模式接入devops平台
        // saas: saas模式接入蓝鲸底座
        var MODE_CONFIG = '__BK_REPO_DEPLOY_MODE__' || 'standalone'
        // 是否显示项目设置菜单
        var SHOW_PROJECT_CONFIG_MENU = MODE_CONFIG !== 'ci'
        // 是否显示制品分析菜单
        var SHOW_ANALYST_MENU = '__BK_REPO_SHOW_ANALYST_MENU__' || 'false'
        SHOW_ANALYST_MENU = SHOW_ANALYST_MENU === 'true'
        var ADD_FROM_LOGOUT = '__IS_FROM_LOGOUT__' || 'not'
        // 区分社区版与内部版  community -> 社区   tencent -> 内部
        var RELEASE_MODE = '__BK_REPO_RELEASE_MODE__' || 'community'
        // 存放文档中心的url
        var DOC_URL = '__BK_REPO_DOCS_FQDN__'|| 'https://bk.tencent.com/docs'
        // displayName的网关URL
        var API_BASE_URL =  (/^https?/.test('__BK_REPO_PAAS_FQDN__') ? 'https://bkapi.' : 'http://bkapi.')
                            + ('__BK_REPO_PAAS_FQDN__').split("://")[1] + '/api/bk-user-web/prod'
        // 是否开启多租户模式
        var BK_REPO_ENABLE_MULTI_TENANT_MODE = '__BK_REPO_ENABLE_MULTI_TENANT_MODE__' || 'false'

    </script>
    <script>
        const inIframe = self !== top
        // iframe内使用ci模式
        if (MODE_CONFIG === 'saas' && inIframe) {
            MODE_CONFIG = 'ci'
        }

        window.getLoginUrl = function (cUrl = location.href) {
            if (/\{+curl\}+/i.test(LOGIN_SERVICE_URL)) {
                return LOGIN_SERVICE_URL.replace(/\{+curl\}+/i, encodeURIComponent(cUrl))
            } else if (/=%s/.test(LOGIN_SERVICE_URL)) {
                return LOGIN_SERVICE_URL.replace(/%s/, cUrl)
            } else {
                const loginUrl = new URL(LOGIN_SERVICE_URL)
                if (/=$/.test(loginUrl.search)) {
                    return LOGIN_SERVICE_URL + cUrl
                } else {
                    loginUrl.searchParams.set('c_url', cUrl)
                    return loginUrl.href
                }
            }
        }

        const search = new URL(location.href).searchParams // redirect/token/download/?preview
        const redirect = search.get('redirect')
        if (redirect) {
            search.delete('redirect')

            const redirectUrl = new URL(/^https?/.test(redirect) ? redirect : location.origin + redirect)
            if (!search.toString()) {
                // 兼容之前的预览链接格式
                redirectUrl.searchParams.set('preview', true)
            } else {
                search.forEach((val, key) => {
                    redirectUrl.searchParams.set(key, val)
                })
            }

            const xhr = new XMLHttpRequest()
            xhr.onreadystatechange = function (e) {
                const { readyState, status } = e.currentTarget
                // 已登录
                if (readyState === 4) {
                    if (status === 200) {
                        window.open(redirectUrl.href, '_self')
                        mountApp()
                    } else if (status === 401) {
                        if (MODE_CONFIG === 'ci' || MODE_CONFIG === 'saas') {
                            // ci登录逻辑
                            location.href = window.getLoginUrl()
                        } else {
                            // 保存操作链接，登陆后调用
                            sessionStorage.setItem('afterLogin', redirectUrl.href)
                            window.login = true
                            mountApp()
                        }
                    } else {
                        let message
                        switch (status) {
                            case 403:
                                message = '未获得文件访问权限或链接已失效'
                                break
                            case 404:
                                message = '文件不存在'
                                break
                            default:
                                message = '服务异常，请稍后重试'
                        }
                        printError(status, message)
                    }
                }
            }
            xhr.withCredentials = true
            xhr.open('HEAD', redirectUrl.href)
            xhr.send()
        } else {
            mountApp()
        }

        function  mountApp () {
            const app = document.createElement('div')
            app.setAttribute('id', 'app')
            document.body.append(app)
            if (window.repositoryVue) window.repositoryVue.$mount('#app')
        }

        function printError (status, message) {
            document.writeln(
                '<div style="height: 100%; width: 100%; display: flex; align-items: center; justify-content: center; flex-direction: column;">'
                + '<img src="/ui/'
                + status
                + '.png" />'
                + '<p style="font-size: 20px; color: #979797; margin: 32px 0;">'
                + message
                + '</p>'
                + '</div>'
            )
        }

        function initWaterMark (param) {
            watermark.init({
                watermark_txt: param.watermarkTxt,
                watermark_x: 0,
                watermark_y: 0,
                watermark_rows: 0,
                watermark_cols: 0,
                watermark_x_space: Number(param.watermarkXSpace),
                watermark_y_space: Number(param.watermarkYSpace),
                watermark_font: param.watermarkFont,
                watermark_fontsize: param.watermarkFontsize,
                watermark_color: param.watermarkColor,
                watermark_alpha: param.watermarkAlpha,
                watermark_width: Number(param.watermarkWidth),
                watermark_height: Number(param.watermarkHeight),
                watermark_angle: Number(param.watermarkAngle)
            })
        }

        function resetWaterMark() {
            watermark.init({
                watermark_txt: '  '
            })
        }
    </script>
    <script src="/ui/libs/bk-magic-vue/bk-magic-vue.min.js"></script>
</body>
</html>
